# coding: utf-8


from django.db import models

# Create your models here.
#常用量
# 行业权重
INDUSTRY_WEIGHT_CHOICES = (
    (u'9',u'非常重要'),  #
    (u'7',u'重要'),
    (u'5',u'普通'),
    (u'3',u'不重要'),
    (u'1',u'非常不重要'),
)
# 联系在行业中的层次
PERSON_INDUSTRY_WEIGHT_CHOICES = (
    (u'9',u'领袖'), #乔布斯
    (u'7',u'很有影响力'), #张朝阳
    (u'5',u'有一定的影响力'),
    (u'3',u'普通'),
    (u'1',u'没有影响力'),
    )
# 专业列表
FIELD_TYPE_CHOICES = (
    (u'软件开发',u'软件开发'),
    (u'测试',u'测试'),
    (u'项目管理',u'项目管理'),
    (u'设计',u'设计'),
    (u'产品',u'产品'),
    (u'市场',u'市场'),
    (u'销售',u'销售'),
    (u'运营',u'运营'),
    (u'法律',u'法律'),
    (u'财务',u'财务'),
    (u'HR',u'HR'),
    (u'教育',u'教育'),
    (u'医疗',u'医疗'),
    (u'金融',u'金融'),
    (u'娱乐',u'娱乐'),
)

# 专业领域权重
FIELD_WEIGHT_CHOICES = (
    ('5',u'极端重要'),  #
    ('4',u'非常重要'),
    ('3',u'重要'),
    ('2',u'普通'),
    ('1',u'不重要'),
)
# 联系人专业领域层次
PERSON_FIELD_LEVEL_CHOICES = (
    ('5',u'泰斗'),
    ('4',u'专家'), # 领域专家
    ('3',u'精通'),
    ('2',u'熟悉'),
    ('1',u'了解'),
)

# 专业领域
class Field(models.Model):
    fieldtype = models.CharField(max_length=20,choices=FIELD_TYPE_CHOICES,verbose_name=u'专业类型')
    fieldname = models.CharField(max_length=20,verbose_name=u'专业名称')
    fieldweight = models.CharField(max_length=10,choices=FIELD_WEIGHT_CHOICES, default='2', verbose_name=u'专业权重')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return u'%s|%s' % (self.fieldtype, self.fieldname)
    class Meta:
        verbose_name = u'专业领域'
        verbose_name_plural = u'专业领域'
        ordering = ['fieldtype']
        unique_together = ("fieldtype", "fieldname")  #联合唯一索引

    # 行业
class Industry(models.Model):
    industryname = models.CharField(max_length=20,unique="true",verbose_name=u'行业名称')
    industryweight = models.CharField(max_length=10,choices=INDUSTRY_WEIGHT_CHOICES, default='5', verbose_name=u'行业权重')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return self.industryname
    class Meta:
        verbose_name = u'行业'
        verbose_name_plural = u'行业'
        ordering = ['industryweight']

# 学校教育
class School(models.Model):
    country = models.CharField(max_length=20, default=u'中国',verbose_name=u'国家')
    province = models.CharField(max_length=20, default=u'北京',verbose_name=u'省')
    city = models.CharField(max_length=20, default=u'北京',verbose_name=u'城市')
    school = models.CharField(max_length=50, verbose_name=u'校名')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return u"%s|%s|%s|%s" %(self.country,self.province,self.city,self.school)
    class Meta:
        verbose_name = u'教育'
        verbose_name_plural = u'教育'
        ordering = ['country','province','city']
        unique_together = ("country", "province","city","school")  #联合唯一索引
# 组织
class Organization(models.Model):
    ORGANIZATION_LEVLE_CHOICES = (  #参考搜狐规模
        (u'10',u'超级跨国公司'),
        (u'9',u'跨国公司'),
        (u'7',u'巨型公司'),
        (u'5',u'大型公司'),
        (u'3',u'中型公司'),
        (u'2',u'小型公司'),
        (u'1',u'微型公司'),
        (u'0',u'皮包公司'),
    )

    organizationname = models.CharField(max_length=50,unique="true", verbose_name=u'组织名称')
    organizationtype = models.CharField(max_length=50, verbose_name=u'组织类型')
    industry = models.ForeignKey(Industry, verbose_name=u'行业')
    organizationlevel = models.CharField(max_length=10,choices=ORGANIZATION_LEVLE_CHOICES,verbose_name=u'组织规模')
    source = models.CharField(max_length=50, verbose_name=u'来源')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return self.organizationname

# 联系人基本信息
class Person(models.Model):
    # 联系人来源类型
    PERSON_SOURCE_CHOICES = (
        (u'同学',u'同学'),
        (u'老乡',u'老乡'),
        (u'同事',u'同事'),
        (u'客户',u'客户'),
        (u'合作伙伴',u'合作伙伴'),
        (u'网友',u'网友'),
        (u'亲戚',u'亲戚'),
        (u'朋友介绍',u'朋友介绍'),
        (u'其他',u'其他'),
    )
    # 性别
    PERSON_SEX_CHOICES = (
        (u'男',u'男'),
        (u'女',u'女'),
    )
    # 血型
    PERSON_BLOOD_CHOICES = (
        (u'A',u'A'),
        (u'B',u'B'),
        (u'AB',u'AB'),
        (u'O',u'O'),
    )
    # 民族
    PERSON_NATION_CHOICES = (
        (u'汉',u'汉'),
        (u'回',u'回'),
    )
    # 婚否
    PERSON_ISMARRYED_CHOICES = (
        (u'单身',u'单身'),
        (u'未婚',u'未婚'),
        (u'已婚',u'已婚'),
        (u'离异',u'离异'),
        (u'丧偶',u'丧偶'),
    )
    # 关系程度
    PERSON_RELATION_CHOICES = (
        (u'9',u'兄弟'),
        (u'7',u'朋友'),
        (u'5',u'熟悉'),
        (u'3',u'认识'),
        (u'2',u'见过'),
        (u'1',u'知道/听说'),
        (u'-3',u'关系不好'),
        (u'-5',u'敌视'),
        (u'-9',u'敌人'),
    )
    # 状态
    PERSON_STATUS_CHOICES = (
        (u'0',u'未处理'),
        (u'1',u'已处理'),
    )
    PERSON_AFFECTION_DEGREE_CHOICES = (
        (u'9', u'严重影响'),     #直接领导
        (u'7', u'影响巨大'),     #间接领导 主要客户
        (u'5', u'有一定影响'),     #其他部门领导 客户
        (u'3', u'有影响'),     #下属
        (u'0', u'没有影响'),     #其他
    )
    name = models.CharField(max_length=20,verbose_name=u'姓名')
    source = models.CharField(max_length=10,choices=PERSON_SOURCE_CHOICES,blank=True, verbose_name=u'来源类型')  #枚举
    relation = models.CharField(max_length=10,choices=PERSON_RELATION_CHOICES,default='3',blank=True, verbose_name=u'关系程度')  #枚举
    status = models.CharField(max_length=10,choices=PERSON_STATUS_CHOICES,default='0',blank=True, verbose_name=u'处理状态')
    affect_to_me = models.CharField(max_length=10,choices=PERSON_AFFECTION_DEGREE_CHOICES,blank=True,default=0, verbose_name=u'对我的影响力')
    affect_to_person = models.CharField(max_length=10,choices=PERSON_AFFECTION_DEGREE_CHOICES,blank=True,default=0, verbose_name=u'对联系人的影响力')
    field = models.ManyToManyField(Field, blank=True, verbose_name=u'涉及领域')

    # 个人信息
    ename = models.CharField(max_length=20, blank=True, verbose_name=u'英文名')
    nickname = models.CharField(max_length=20, blank=True, verbose_name=u'昵称')
    sex = models.CharField(max_length=10,choices=PERSON_SEX_CHOICES,blank=True, verbose_name=u'性别')
    blood = models.CharField(max_length=10,choices=PERSON_BLOOD_CHOICES, blank=True, verbose_name=u'血型')
    birthday = models.DateField(null=True, blank=True, verbose_name=u'生日')
    nation = models.CharField(max_length=10,choices=PERSON_NATION_CHOICES, default='汉', blank=True, verbose_name=u'民族')
    passid = models.CharField(max_length=20, blank=True, verbose_name=u'护照号')
    idcard = models.CharField(max_length=20, blank=True, verbose_name=u'身份证')
    ismarryed = models.CharField(max_length=10,choices=PERSON_ISMARRYED_CHOICES,blank=True, verbose_name=u'婚否')

    qq = models.CharField(max_length=60, blank=True, verbose_name=u'QQ') # Field name made lowercase.
    msn = models.EmailField(blank=True, verbose_name=u'MSN') # Field name made lowercase.
    mobile = models.CharField(max_length=20, blank=True, verbose_name=u'手机')
    othermbile = models.CharField(max_length=20, blank=True, verbose_name=u'备份手机')
    phone = models.CharField(max_length=20, blank=True, verbose_name=u'工作座机')
    homephone = models.CharField(max_length=20, blank=True, verbose_name=u'座机（家）')
    fax = models.CharField(max_length=20, blank=True, verbose_name=u'传真')
    personalemail = models.EmailField(blank=True, verbose_name=u'个人邮箱')
    pubemail = models.EmailField( blank=True, verbose_name=u'工作邮箱')

    natianalcountry = models.CharField(max_length=20, blank=True, verbose_name=u'出生国家')
    natianalprovince = models.CharField(max_length=20, blank=True, verbose_name=u'出生国家')
    natianalcity = models.CharField(max_length=20, blank=True, verbose_name=u'出生城市') # Field name made lowercase.

    addrcountry = models.CharField(max_length=20, blank=True, verbose_name=u'国家')
    addrprovince = models.CharField(max_length=20, blank=True, verbose_name=u'省')
    addrcity = models.CharField(max_length=20, blank=True, verbose_name=u'城市')
    addr = models.CharField(max_length=100, blank=True, verbose_name=u'地址')
    postcode = models.CharField(max_length=10, blank=True, verbose_name=u'邮编')

    car = models.CharField(max_length=50, blank=True, verbose_name=u'车牌')
    bank = models.CharField(max_length=100, blank=True, verbose_name=u'银行账号')
    interest = models.TextField(max_length=600, blank=True, verbose_name=u'爱好')
    taboo = models.TextField(max_length=600, blank=True, verbose_name=u'忌讳')
    wish = models.TextField(max_length=600, blank=True, verbose_name=u'心愿')
    health = models.TextField(max_length=600, blank=True, verbose_name=u'健康')
    pleased = models.TextField(max_length=600, blank=True, verbose_name=u'得意的事')
    tag = models.CharField(max_length=100, blank=True, verbose_name=u'标签')

    school = models.ManyToManyField(School, blank=True, verbose_name=u'教育')

    def __unicode__(self):
        return u'%s|%s' %(self.name,self.source)
    class Meta:
        verbose_name = u'联系人'
        verbose_name_plural = u'联系人'
        ordering = ['name']

# 联系人行业地位
class PersonIndustry(models.Model):
    personid = models.ForeignKey(Person,verbose_name=u'联系人')
    industryid = models.ForeignKey(Industry,verbose_name=u'行业')
    level = models.CharField(max_length=10, choices=PERSON_INDUSTRY_WEIGHT_CHOICES, default=3, verbose_name=u'行业地位')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return u'%s|%s|%s' % (self.personid.name,self.industryid.industryname,str(self.level))
    class Meta:
        verbose_name = u'联系人行业地位'
        verbose_name_plural = u'联系人行业地位'
        ordering = ['personid']
        unique_together = ("personid", "industryid")

# 联系人专业层次
class PersonField(models.Model):
    person = models.ForeignKey(Person,verbose_name=u'联系人')
    field = models.ForeignKey(Field,verbose_name=u'专业领域')
    level = models.CharField(max_length=10, choices=PERSON_FIELD_LEVEL_CHOICES, default=2, verbose_name=u'专业层次')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return u'%s|%s|%s|%s' % (self.person.name,self.field.fieldtype,self.field.fieldname,str(self.level))
    class Meta:
        verbose_name = u'联系人专业层次'
        verbose_name_plural = u'联系人专业层次'
        ordering = ['person']
        unique_together = ("person", "field")
        db_table = u'person_person_field'
        managed = False

#class PersonField(models.Model):
#personid = models.ForeignKey(Person,verbose_name=u'联系人',limit_choices_to = {'source': '同学'})
#fieldid = models.ForeignKey(Field,verbose_name=u'专业领域')
#level = models.CharField(max_length=10, choices=PERSON_FIELD_LEVEL_CHOICES, default=2, verbose_name=u'专业层次')
#desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
#def __unicode__(self):
#    return u'%s|%s|%s|%s' % (self.personid.name,self.fieldid.fieldtype,self.fieldid.fieldname,str(self.level))
#class Meta:
#    verbose_name = u'联系人专业层次'
#    verbose_name_plural = u'联系人专业层次'
#    ordering = ['personid']
#    unique_together = ("personid", "fieldid")

class PersonOrganiaztion(models.Model):
    PERSON_ORGANIZATION_LEVLE_CHOICES = (  #参考搜狐规模
        (u'10',u'老板'),   #老板
        (u'9',u'总裁'),
        (u'8',u'副总裁'),
        (u'7',u'高级总监'),
        (u'6',u'总监'),
        (u'5',u'高级经理'),
        (u'4',u'经理'),
        (u'3',u'主管'),
        (u'2',u'高级员工'),
        (u'1',u'中级员工'),
        (u'0',u'初级员工'),
    )
    personid = models.ForeignKey(Person,verbose_name=u'联系人')
    organizationid = models.ForeignKey(Organization,verbose_name=u'组织')
    level = models.CharField(max_length=10, choices=PERSON_ORGANIZATION_LEVLE_CHOICES,verbose_name=u'专业层次')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return u'%s|%s|%s' % (self.personid.name,self.organizationid.organizationname,str(self.level))
    class Meta:
        verbose_name = u'联系人组织层次'
        verbose_name_plural = u'联系人组织层次'
        ordering = ['organizationid']
        unique_together = ("personid", "organizationid")

# 联系人之间关系

class PersonLinkPerson(models.Model):
    PERSON_PERSON_RELATION__TYPE_CHOICES = (
        (u'父子',u'父子'),
        (u'夫妻',u'夫妻'),
    )
    person1 = models.ForeignKey(Person,verbose_name=u'联系人1', related_name="person1")
    person2 = models.ForeignKey(Person,verbose_name=u'联系人2', related_name="person2")
    linktype = models.CharField(max_length=20,choices=PERSON_PERSON_RELATION__TYPE_CHOICES, verbose_name=u'关系')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return u'%s|%s|%s' % (self.person1.name,self.person2.name,self.linktype)
    class Meta:
        verbose_name = u'联系人之间关系'
        verbose_name_plural = u'联系人之间关系'
        ordering = ['person1']
        unique_together = ("person1", "person2")

# todo: 活动中如何显示参与人
class party(models.Model):
    PERSON_PARTY_TYPE_CHOICES = (
        (u'吃饭',u'吃饭'),
        (u'KTV',u'KTV'),
        (u'德州扑克',u'德州扑克'),
    )
    originator = models.ForeignKey(Person,verbose_name=u'发起人', related_name="originator")
    persons = models.ManyToManyField(Person,related_name="persons",verbose_name=u'参与人')
    payperson = models.ForeignKey(Person,related_name="payperson",verbose_name=u'付款人')
    when = models.DateField(verbose_name=u'时间')
    place = models.CharField(max_length=20,verbose_name=u'地点')
    partytype = models.CharField(max_length=20,choices=PERSON_PARTY_TYPE_CHOICES, verbose_name=u'活动类型')
    cash = models.IntegerField(max_length=8,verbose_name=u'金额')
    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
    def __unicode__(self):
        return u'%s|%s|%s' % (self.when,self.partytype,self.originator.name)
    class Meta:
        verbose_name = u'活动'
        verbose_name_plural = u'活动'
        ordering = ['when']
        unique_together = ("when", "place","originator")

# 实验一下维护magytomany 关系，这样可以在联系人设置有那些关系，然后在对应的class中设置其他属性
#class personschool(models.Model):
#    id = models.IntegerField(primary_key=True)
#    person = models.ForeignKey(Person)
#    school = models.ForeignKey(School)
#    desc = models.TextField(max_length=600, blank=True,verbose_name=u'描述')
##    def __unicode__(self):
##        return u'%s|%s|%s' % (self.partytype,self.originator.name)
#    class Meta:
#        verbose_name = u'联系人学校关系'
#        verbose_name_plural = u'联系人学校关系'
#        db_table = u'person_person_school'
#        managed = False